package com.treasure.mall.zbqmallmanager.controller.withdraw;

import com.treasure.mall.biz.bo.BaseConfig;
import com.treasure.mall.biz.bo.config.WithdrawConfig;
import com.treasure.mall.biz.dto.condition.withdraw.WithdrawSearchCondition;
import com.treasure.mall.biz.dto.withdraw.UpdateWithdrawStatusDTO;
import com.treasure.mall.biz.dto.withdraw.WithdrawConfigDTO;
import com.treasure.mall.biz.entity.auth.SystemManager;
import com.treasure.mall.biz.entity.withdraw.WithdrawPO;
import com.treasure.mall.biz.service.core.BaseConfigService;
import com.treasure.mall.biz.service.core.withdraw.WithdrawService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.withdraw.WithdrawEnums;
import com.treasure.mall.zbq.common.ienum.withdraw.WithdrawLogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author zoul
 * @version 1.0
 * @date 2020/6/14 17:09
 * @Description
 */
@Controller
@RequestMapping("/withdraw")
public class WithdrawController {
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private BaseConfigService baseConfigService;

    /**
     * 提现申请列表
     *
     * @param condition 查询实体
     * @param pageIndex 页码
     * @param model
     * @return
     */
    @GetMapping(value = "/list")
    public String list(@ModelAttribute("condition") WithdrawSearchCondition condition,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex, Model model) {

        condition.setWithdrawType(WithdrawEnums.WithdrawType.WITHDRAW_BALANCE);
        model.addAttribute("withdrwaType", 0);
        return listPage(pageIndex, condition, model);
//        if (type == 0) {
//            condition.setWithdrawType(WithdrawEnums.WithdrawType.WITHDRAW_BALANCE);
//        } else {
//            condition.setWithdrawType(WithdrawEnums.WithdrawType.WITHDRAW_GOODS);
//        }
//
//        Page<WithdrawPO> applyPos = withdrawService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
//        model.addAttribute("pageIndex", pageIndex);
//        model.addAttribute("totalRecords", applyPos.getTotalElements());
//        model.addAttribute("totalPages", applyPos.getTotalPages());
//        model.addAttribute("list", applyPos.getContent());
//        model.addAttribute("type,", type);
//        return "withdraw/list";
    }

    @GetMapping(value = "/list/type")
    public String listType(@ModelAttribute("condition") WithdrawSearchCondition condition,
                           @RequestParam(required = false, defaultValue = "1") int pageIndex, Model model) {
        condition.setWithdrawType(WithdrawEnums.WithdrawType.WITHDRAW_GOODS);
        model.addAttribute("withdrwaType", 1);
        return listPage(pageIndex, condition, model);

    }

    private String listPage(Integer pageIndex, WithdrawSearchCondition condition, Model model) {
        Page<WithdrawPO> applyPos = withdrawService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", applyPos.getTotalElements());
        model.addAttribute("totalPages", applyPos.getTotalPages());
        model.addAttribute("list", applyPos.getContent());
        return "withdraw/list";
    }


    /**
     * 获取提现配置
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/config")
    public String getConfig(Model model) {
        BaseConfig baseConfig = baseConfigService.getConfig();
        model.addAttribute("withdrawConfig", baseConfig.getWithdrawConfig());
        return "withdraw/config";
    }

    /**
     * 修改提现状态
     *
     * @param manager
     * @param dto
     * @return
     */
    @PostMapping(value = "/updateStatus")
    @ResponseBody
    public ApiResult updateStatus(@AuthenticationPrincipal SystemManager manager, UpdateWithdrawStatusDTO dto) {
        dto.setLogType(WithdrawLogType.UPDATE_STATUS);
        dto.setOpId(manager.getUserId());
        dto.setOpName(manager.getUsername());
        withdrawService.updateWithdrawStatus(dto);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存提现配置
     *
     * @param manager
     * @param dto
     * @return
     */
    @PostMapping(value = "/saveWithdrawConfig")
    @ResponseBody
    public ApiResult saveWithdrawConfig(@AuthenticationPrincipal SystemManager manager, WithdrawConfigDTO dto) {
        dto.setLogType(WithdrawLogType.UPDATE_WITHDRAW_CONFIG);
        dto.setOpId(manager.getUserId());
        dto.setOpName(manager.getUsername());
        WithdrawConfig withdrawConfig = new WithdrawConfig();
        withdrawConfig.setBalanceMaxAmount(dto.getBalanceMaxAmount());
        withdrawConfig.setBalanceMinAmount(dto.getBalanceMinAmount());
        withdrawConfig.setBalanceWithdrawNum(dto.getBalanceWithdrawNum());
        withdrawConfig.setBalanceWithdrawRatio(dto.getBalanceWithdrawRatio());
        withdrawConfig.setGoodsMaxAmount(dto.getGoodsMaxAmount());
        withdrawConfig.setGoodsMinAmount(dto.getGoodsMinAmount());
        withdrawConfig.setGoodsWithdrawNum(dto.getGoodsWithdrawNum());
        withdrawConfig.setGoodsWithdrawRatio(dto.getGoodsWithdrawRatio());
        BaseConfig baseConfig = baseConfigService.getConfig();
        baseConfig.setWithdrawConfig(withdrawConfig);
        baseConfigService.save(baseConfig);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }
}
